package net.ddns.xcyyds.kungfu.procedures;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.advancements.Advancement;

import net.ddns.xcyyds.kungfu.network.KungFuModVariables;

import java.util.Iterator;

public class UseArm1Procedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new KungFuModVariables.PlayerVariables())).howToUse) {
			if (entity.getPersistentData().getBoolean("kungFuUse")) {
				entity.getPersistentData().putBoolean("kungFuUse", (false));
				if (entity instanceof ServerPlayer _player) {
					Advancement _adv = _player.server.getAdvancements().getAdvancement(new ResourceLocation("kung_fu:kung_fu_1"));
					AdvancementProgress _ap = _player.getAdvancements().getOrStartProgress(_adv);
					if (!_ap.isDone()) {
						Iterator _iterator = _ap.getRemainingCriteria().iterator();
						while (_iterator.hasNext())
							_player.getAdvancements().award(_adv, (String) _iterator.next());
					}
				}
				FirstProcedure.execute(world, x, y, z, entity);
				NewNBTProcedure.execute(entity);
			} else {
				entity.getPersistentData().putBoolean("kungFuUse", (true));
				entity.getPersistentData().putDouble("NO", 1);
				SoundProcedure.execute(world, x, y, z, 0.9f, 1f);
				if (entity instanceof Player _player && !_player.level.isClientSide())
					_player.displayClientMessage(new TextComponent("-_-_-_-"), (true));
			}
		} else {
			entity.getPersistentData().putBoolean("kungFuUse", (true));
			entity.getPersistentData().putDouble("NO", 1);
			SoundProcedure.execute(world, x, y, z, 0.9f, 1f);
			if (entity instanceof Player _player && !_player.level.isClientSide())
				_player.displayClientMessage(new TextComponent("-_-_-_-"), (true));
		}
	}
}
